import axios from 'axios'
import { useUserStore } from '@/stores'

const baseURL = 'http://localhost:3000'
const timeout = 1000

const instance = axios.create({
  baseURL,
  timeout,
})

// 请求拦截器
instance.interceptors.request.use(
  (config) => {
    const userStore = useUserStore()
    if (userStore.token) config.headers.Authorization = 'Bearer ' + userStore.token
    return config
  },
  (error) => {
    return Promise.reject(error)
  },
)

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  },
)

export default instance
